package com.lanju.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.TopicDao;
import com.lanju.entity.Topic;
import com.lanju.service.TopicService;

@Service
public class TopicServiceImpl implements TopicService{
	@Autowired
	private TopicDao topicDao;

	@Override
	public Map<String, Object> getTopicList(Map<String, Object> map) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			result.put("rows", topicDao.getTopicList(map));
			result.put("total", topicDao.getTopicListCount(map));
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> saveTopic(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<>();
		try {
			Topic t = new Topic();
			t.setCreate_time(new Date());
			t.setCreator(map.get("userId").toString());
			t.setStatus(Integer.valueOf(map.get("status").toString()));
			t.setTopic_img(map.get("topic_img").toString());
			t.setTopic_name( map.get("topic_name").toString());
			t.setTopic_url( map.get("topic_url").toString());
			t.setUpdate_time(new Date());
			t.setUpdator(map.get("userId").toString());
			topicDao.saveTopic(t);
			result.put("success", true);
			result.put("msg", "新增成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "新增失败");
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateTopic(Topic topic) {
		Map<String, Object> result = new HashMap<>();
		try {
			topicDao.updateTopic(topic);
			result.put("success", true);
			result.put("msg", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "修改失败");
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> delTopic(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<>();
		try {
			topicDao.delTopic1(Integer.valueOf(map.get("topic_id").toString()));
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "删除失败");
		}
		return result;
	}

	@Override
	public Map<String, Object> getTopic(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<>();
		try {
			result.put("rows", topicDao.getTopic(Integer.valueOf(map.get("topic_id").toString())));
			result.put("success", true);
			result.put("msg", "获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "获取失败");
		}
		return result;
	}

}
